package com.dongjiantong.user.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.common.model.RoleModel;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.user.mapper.OrgUserMapper;
import com.dongjiantong.user.model.RegionAccount;
import com.dongjiantong.user.model.RoleAppUser;
import com.dongjiantong.user.model.dtos.OrgUserDto;
import com.dongjiantong.user.service.OrgUserService;
import com.dongjiantong.user.service.RoleAppUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author cao
 * @create 2017-11-14 下午4:57
 **/
@Slf4j
@Service(version = "1.0.1")
public class OrgUserServiceImpl  extends ServiceMybatis<OrgUserDto> implements OrgUserService {

    @Resource
    private OrgUserMapper orgUserMapper;

    @Autowired
    private RoleAppUserService roleAppUserService;



   @Override
   public PageInfo<OrgUserDto> getBossOrgUserListByPage(Integer pageNum,
                                              Integer pageSize,
                                              Map<String,Object> searchMap)
    {


        PageHelper.startPage(pageNum, pageSize);
        List<OrgUserDto> userList =orgUserMapper.getBossOrgUserListByPage(searchMap);

        PageInfo<OrgUserDto> pageInfo = new PageInfo<OrgUserDto>(userList);

         List<OrgUserDto> orgUserDtoList = Lists.newArrayList();



        for (OrgUserDto orgUserDto:userList) {


            //APP角色
            List<RoleAppUser> roleAppUserList =  roleAppUserService.queryRoleAppByUserId(orgUserDto.getUserId());

            List<RoleModel> roleModelList = Lists.newArrayList();
            RoleModel roleModel;
            for (RoleAppUser roleAppUser:roleAppUserList) {

                roleModel = new RoleModel();
                roleModel.setRoleCode(roleAppUser.getAppRole());
                roleModel.setRoleName(roleAppUser.getAppRoleName());

                roleModelList.add(roleModel);
            }

            orgUserDto.setAppRole(roleModelList);
            orgUserDtoList.add(orgUserDto);



            //WEB角色


        }
        pageInfo.setList(orgUserDtoList);


        return  pageInfo;
    }

}
